package com.work.controller;

import com.work.bean.*;
import com.work.dao.StudentDao;
import com.work.dao.UserDao;
import com.work.dao.impl.StudentDaoImpl;
import com.work.dao.impl.UserDaoImpl;
import com.work.dto.StudentClass;
import com.work.service.ClassService;
import com.work.service.NoticeService;
import com.work.service.StudentService;
import com.work.service.impl.ClassServiceImpl;
import com.work.service.impl.NoticeServiceImpl;
import com.work.service.impl.StudentServiceImpl;
import com.work.utils.WebUtils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class StudentServlet extends BaseServlet{
    StudentService studentService = new StudentServiceImpl();
    UserDao userDao = new UserDaoImpl();
    ClassService classService = new ClassServiceImpl();
    NoticeService noticeService = new NoticeServiceImpl();
    protected void queryStudent(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        List<Student> students = studentService.queryStudent();
        req.setAttribute("students",students);
        req.getRequestDispatcher("/pages/admin/student/admin_student.jsp").forward(req, resp);
    }
    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
       Student student = WebUtils.copyParamToBean(req.getParameterMap(),new Student());
       if(((Student)req.getSession().getAttribute("student")) == null){
           studentService.updateStudent(student);
           resp.sendRedirect(req.getContextPath()+"/StudentServlet?action=queryStudent");
       }else {
           int id = WebUtils.parseInt(((Student)req.getSession().getAttribute("student")).getId()+"",0);
           student.setId(id);
           int uid = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
           student.setuId(uid);
           studentService.updateStudent(student);
           req.getSession().setAttribute("student",student);
           resp.sendRedirect(req.getContextPath() + "/StudentServlet?action=query");
       }
    }
    protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        Student student = com.work.utils.WebUtils.copyParamToBean(req.getParameterMap(),new Student());
        studentService.deleteStudent(student.getId());
        resp.sendRedirect(req.getContextPath()+"/StudentServlet?action=queryStudent");
    }
    protected void saveStudent(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        Student student = WebUtils.copyParamToBean(req.getParameterMap(), new Student());
        if(((User)req.getSession().getAttribute("user")).getType().equals("0")){
        studentService.saveStudent(student);
        resp.sendRedirect(req.getContextPath()+"/StudentServlet?action=queryStudent");
        }else{
            int uid = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
            student.setuId(uid);
            studentService.saveStudent(student);
            int id = studentService.query().getId();
            student.setId(id);
            req.getSession().setAttribute(  "student",student);
            req.getRequestDispatcher("/pages/student/student.jsp").forward(req, resp);
        }

    }
    protected void queryStudentById(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Student student = studentService.queryStudentById(id);
        req.setAttribute("student",student);
        req.getRequestDispatcher("/pages/admin/student/admin_student_edit.jsp").forward(req, resp);

    }
    protected void updatePassword(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        int id = WebUtils.parseInt(((User)req.getSession().getAttribute("user")).getId()+"",0);
        String password = req.getParameter("password");
        userDao.update(new User(id,null,password,null,null));
        User user = ((User)req.getSession().getAttribute("user"));
        user.setUpass(password);
        req.getSession().setAttribute("user",user);
        req.getRequestDispatcher("/pages/student/student.jsp").forward(req, resp);
    }
    protected void query(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        if (((Student) req.getSession().getAttribute("student")) == null) {
            req.setAttribute("studentClass", null);
            req.getRequestDispatcher("/pages/student/student_update.jsp").forward(req, resp);
        } else {
            StudentClass studentClass = classService.query(((Student) req.getSession().getAttribute("student")));
            req.setAttribute("studentClass", studentClass);
            Student student = studentService.queryStudentById(studentClass.getId());
            req.getSession().setAttribute("student", student);
            req.getRequestDispatcher("/pages/student/student_update.jsp").forward(req, resp);
        }
    }
}
